package com.coda.service.impl;

import com.coda.adminVo.system.user.SystemUserItemVo;
import com.coda.constant.MessageConstant;
import com.coda.constant.StatusConstant;
import com.coda.context.BaseContext;
import com.coda.entity.SystemPost;
import com.coda.entity.SystemUser;
import com.coda.exception.AdminException;
import com.coda.exception.StatusIsStartException;
import com.coda.exception.UsernameExistsException;
import com.coda.mapper.SystemUserMapper;
import com.coda.result.PageResult;
import com.coda.service.SystemUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.aspectj.bridge.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SystemUserServiceImpl implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;
    /**
     *条件分页查询后台用户列表
     * @return
     */
    @Override
    public PageResult getPageList(Integer current,Integer size,SystemUser systemUser) {
        //设置分页参数
        PageHelper.startPage(current,size);
        //条件查询
        Page<SystemUserItemVo>  page = systemUserMapper.getPageList(systemUser);
        PageResult pageResult = new PageResult( page.getTotal(),  page.getResult());
        return pageResult;
    }

    /**
     * 根据ID查询后台用户信息
     * @param id
     * @return
     */
    @Override
    public SystemUserItemVo getById(Long id) {

        SystemUserItemVo systemUserItemVo = systemUserMapper.getById(id);
        return systemUserItemVo;
    }

    /**
     * 保存或更新后台用户信息
     * @param systemUser
     */
    @Override
    public void saveOrUpdate(SystemUser systemUser) {

        //将密码md5加密
        if (systemUser.getPassword() !=null ){
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }

        //根据id判断是插入还是更新操作
        if (systemUser.getId() == null){
            systemUser.setStatus(StatusConstant.DISABLE);
            systemUserMapper.insert(systemUser);

        }else {
            //修改员工 判断当前用户是否是管理员，如果不是不能修改用户信息
            SystemUserItemVo byId = systemUserMapper.getById(BaseContext.getCurrentId());
            if (byId.getType() != 0){
                throw new AdminException(MessageConstant.NOT_ADMIN);
            }
            systemUserMapper.update(systemUser);
        }

    }

    /**
     * 根据用户名查询用户数量
     * @param username
     */
    @Override
    public void isUsernameExists(String username) {
        Integer count = systemUserMapper.getCountUsername(username);
        if (count>0){
            throw new UsernameExistsException(MessageConstant.USERNAME_IS_EXISTS);
        }
    }

    /**
     * 修改后台用户状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatusByUserId(Long id, Integer status) {
        SystemUser systemUser = new SystemUser();
        systemUser.setId(id);
        systemUser.setStatus(status);
        systemUserMapper.update(systemUser);
    }

    /**
     * 根据id删除后台用户信息
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //判断用户状态
        SystemUserItemVo byId = systemUserMapper.getById(id);
        if (byId.getStatus() != 0){
            throw new StatusIsStartException(MessageConstant.STATUS_IS_START);
        }
        systemUserMapper.deleteById(id);
    }
}
